package com.tqjc.area.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.entity.Stock;
import com.tqjc.area.service.StockService;
import com.tqjc.area.wrapper.RollerWrapper;
import com.tqjc.area.wrapper.StockWrapper;
import com.tqjc.system.core.annotation.Log;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.LogType;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.query.QueryUtils;
import com.tqjc.system.core.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @projectName: BaotouSteel
 * @package: com.tqjc.area.controller
 * @className: StockController
 * @author: Patienyon
 * @description: TODO
 * @date: 2023/7/2 23:34
 * @version: 1.0
 */
@Controller
@Slf4j
@RequestMapping("/stock")
public class StockController {

    @Resource
    StockService stockService;


    //辊道管理主页
    @RequestMapping("/index")
    public String index() {
        return "baotou-steel/AreaManage/StockManage.html";
    }


    /**
     * 获取表格数据 树形结构
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/datagrid")
    @ResponseBody
    public void datagrid(Stock stock, PageInfo<Stock> page, HttpServletRequest request, HttpServletResponse response) {
        //拼接查询条件
        QueryWrapper<Stock> queryWrapper = QueryUtils.installQueryWrapperRow(stock, request.getParameterMap());

        //执行查询
        IPage<Stock> lstResult = stockService.page(page.getPageEntity(), queryWrapper);

        //结果处理,直接写到客户端
        ResponseUtil.write(response, new StockWrapper(lstResult).wrap());
    }

    //辊道增加页面主页

    /**
     * 跳转到页面
     *
     * @param rowInfo
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Stock rowInfo, Model model) {

        if (rowInfo.getId() != null) {
            Stock stock = stockService.queryById(rowInfo.getId());
            model.addAttribute("stock", stock);
        }


        return "baotou-steel/AreaManage/StockAddManage.html";
    }




    /**
     * 保存方法
     *
     * @param rowInfo
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    @Log(type = LogType.save, name = "辊道区域表", memo = "新增或编辑保存了辊道区域表信息")
    public AjaxJson save(Stock rowInfo) {
        AjaxJson j = new AjaxJson();

        try {
            //新增保存
            if (rowInfo.getId() == null) {
                //
                stockService.insert(rowInfo);
            }
            //编辑保存
            else {
                QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", rowInfo.getId());
                stockService.update(rowInfo,queryWrapper);
//                rollerService.saveOrUpdate(rowInfo);
//                rollerService.update(rowInfo);
            }

        } catch (Exception e) {
            log.error("保存辊道信息，错误信息:" + e.getMessage(),e);
            j.setSuccess(false);
            e.printStackTrace();
        }

        return j;
    }


    /**
     * 删除操作
     *
     * @param rowInfo
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Log(type = LogType.del, name = "辊道区域表", memo = "删除了辊道区域表信息")
    public AjaxJson delete(Stock rowInfo) {
        AjaxJson j = new AjaxJson();
        try {
//            QueryWrapper<Roller> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("ID", rowInfo.getId());
//            tTgsAreaListMapper.delete(queryWrapper);
            stockService.deleteById(rowInfo.getId());

        } catch (Exception e) {
            log.error("删除辊道表出错，错误信息:" + e.getMessage(),e);
            j.setSuccess(false);
            e.printStackTrace();
        }

        return j;
    }
}
